home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_01 / allison / readbase.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-02  |  675 b   |  33 lines

  1. LISTING 10 - Uses strtol() to read numbers in different bases
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. main()
  7. {
  8.     char *input = "101 123 45678 90abc g";
  9.     char *nextp = input;
  10.     long bin, oct, dec, hex, beyond;
  11.  
  12.     bin = strtol(nextp,&nextp,2);
  13.     oct = strtol(nextp,&nextp,8);
  14.     dec = strtol(nextp,&nextp,10);
  15.     hex = strtol(nextp,&nextp,16);
  16.     beyond = strtol(nextp,&nextp,17);
  17.  
  18.     printf("bin = %ld\n",bin);
  19.     printf("oct = %lo\n",oct);
  20.     printf("dec = %ld\n",dec);
  21.     printf("hex = %lx\n",hex);
  22.     printf("beyond = %ld\n",beyond);
  23.     return 0;
  24. }
  25.  
  26. /* Output: */
  27. bin = 5
  28. oct = 123
  29. dec = 45678
  30. hex = 90abc
  31. beyond = 16
  32.  
  33.